package com.xiyou.luckdraw.web.controller;

import com.google.common.collect.Lists;
import com.xiyou.luckdraw.api.dto.login.*;
import com.xiyou.luckdraw.api.service.LoginService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.result.ResponseData;
import com.xiyou.luckdraw.common.result.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: LoginController
 * @Description: 用户登录Controller
 * @author: limingxing
 * @Date: 2019-10-07 21:12
 */
@RestController
@RequestMapping("/")
@Api(value = "LoginController", tags = {"用户登录接口"})
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;

    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    public Object login(@RequestBody LoginRequest loginRequest) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginRequest.getUserName(), loginRequest.getPassword());
        try {
            subject.login(token);
            subject.getSession().setTimeout(86400000);
        } catch (IncorrectCredentialsException exception) {
            exception.printStackTrace();
            return new ResponseUtils().setErrorMsg("用户名或密码错误");
        }
        getUserInfo();

        LoginData login = new LoginData();

        LoginUserInfo userInfo = new LoginUserInfo();
        userInfo.setUserId(getUserId());
        userInfo.setUserName(getUserName());
        login.setUserInfo(userInfo);
        List<RolePermission> rolePermissions = getPermissions();
        List<LoginUserMenu> menus = Lists.newArrayList();
        List<UserPermission> permissions = Lists.newArrayList();

        if (CollectionUtils.isNotEmpty(rolePermissions)) {
            rolePermissions.forEach(rolePermission -> {
                if (rolePermission.getType() == 1) {
                    LoginUserMenu menu = new LoginUserMenu();
                    BeanUtils.copyProperties(rolePermission, menu);
                    menus.add(menu);
                } else if (rolePermission.getType() == 2) {
                    UserPermission permission = new UserPermission();
                    BeanUtils.copyProperties(rolePermission, permission);
                    permissions.add(permission);
                }
            });
        }
        login.setMenus(menus);
        login.setPermissions(permissions);
        return new ResponseUtils().setData(login);
    }

    @PostMapping("loginOut")
    @ResponseBody
    @ApiOperation(value = "退出")
    public Object out() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ResponseUtils().setData("OK");
    }


    /**
     * 修改密码
     *
     * @param param
     * @auther Jiangshijian
     * @date 2019-03-14 20:05
     */
    @ResponseBody
    @Validated
    @PostMapping("password")
    @ApiOperation(value = "修改系统用户密码")
    public ResponseData modifyPwd(@RequestBody UserModifyRequest param) {
        UpdateSysUserResponse response = loginService.modifyPwd(param);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setErrorMsg(-1,response.getMsg());
    }

}
